<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="8">
            <a-form-model-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itempartno">
              <a-input v-model="model.itempartno" placeholder="请输入物料"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="物料版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemrev">
              <a-input v-model="model.itemrev" placeholder="请输入物料版本"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="英文描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="descEn">
              <a-input v-model="model.descEn" placeholder="请输入英文描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="图号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="drawing">
              <a-input v-model="model.drawing" placeholder="请输入图号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="消耗数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtyPer">
              <a-input-number v-model="model.qtyPer" placeholder="请输入消耗数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="本地描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="descLocal">
              <a-input v-model="model.descLocal" placeholder="请输入本地描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" placeholder="请输入单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="上料工序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="op">
              <a-input v-model="model.op" placeholder="请输入需要上料的工序"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否扣减物料批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lottrack">
              <j-dict-select-tag placeholder="请选择是否需要扣减物料批次" v-model="model.lottrack" dictCode="yn" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="是否不分产品共享物料" :labelCol="labelCol"
                               :wrapperCol="wrapperCol" prop="shared">
              <j-dict-select-tag placeholder="请选择是否不分产品共享物料" v-model="model.shared" dictCode="yn" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="告警数量" :labelCol="labelCol"
                               :wrapperCol="wrapperCol" prop="warninglimit">
              <a-input-number v-model="model.warninglimit"
                              placeholder="告警数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="停机数量" :labelCol="labelCol"
                               :wrapperCol="wrapperCol" prop="blocklimit">
              <a-input-number v-model="model.blocklimit"
                              placeholder="请输入停机数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesBomItemSetupModal',
  components: {},
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    }
  },
  data() {
    return {
      title: '操作',
      width: 1500,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        itempartno: [
          { required: true, message: '请输入物料!' }
        ],
        qtyPer: [
          { required: true, message: '请输入消耗数量!' }
        ],
        op: [
          { required: true, message: '请输入需要上料的工序!' }
        ],
        lottrack: [
          { required: true, message: '请输入是否需要扣减物料批次!' }
        ]
      },
      url: {
        add: '/basic/mesBomHeaderSetup/addMesBomItemSetup',
        edit: '/basic/mesBomHeaderSetup/editMesBomItemSetup'
      }

    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.mainId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['partno'] = this.mainId
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    }

  }
}
</script>
